//
//  ByGetScoreFromPayMoreVC.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/11/8.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByGetScoreFromPayMoreVC.h"

// vc
#import "ByGetScoreFromPayDetailVC.h"

// view
#import "ByViewGetScoreFromPayMoreSectionHeader.h"      // 组头

// cell
#import "ByCellGetScoreFromPayStoreItem.h"

@interface ByGetScoreFromPayMoreVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) UIImageView *header;

@end

@implementation ByGetScoreFromPayMoreVC

#pragma mark - vc生命周期
- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"消费得积分";
    
    [self addLeftBackBarButtonItem];
    
    [self createTableView];
}

#pragma mark - 创建视图
- (void)createTableView {
    
    self.table = [[UITableView alloc] init];
    self.table.backgroundColor = BY_BG_COLOR;
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    [self.table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(0, 0, 0, 0)).priorityMedium();
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-0);
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
        }
    }];
    
    self.table.tableHeaderView = self.header;
    
    [self.table registerClass:[ByViewGetScoreFromPayMoreSectionHeader class] forHeaderFooterViewReuseIdentifier:@"sectionHeader"];
    
    [self.table registerClass:[ByCellGetScoreFromPayStoreItem class] forCellReuseIdentifier:@"cell"];
}


#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ByCellGetScoreFromPayStoreItem *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.type = indexPath.section;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return (SCREEN_WIDTH - 20) / 2 + 40 + 30 + 10;
    } else {
        return (SCREEN_WIDTH - 20) / 2 + 40 + 10;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 36;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    ByViewGetScoreFromPayMoreSectionHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"sectionHeader"];
        
    if (section == 0) {
        header.contentView.backgroundColor = [UIColor whiteColor];
        header.title = @"已有积分";
    } else {
        header.contentView.backgroundColor = tableView.backgroundColor;
        header.title = @"消费返利得积分";
    }
    
    return header;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return UIView.new;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:false];
    
    ByGetScoreFromPayDetailVC *vc = [ByGetScoreFromPayDetailVC new];
    [self by_normalPush:vc hasNavi:true];
}

#pragma mark - getter
- (UIImageView *)header {
    
    if (!_header) {
        _header = [UIImageView new];
        _header.frame = CGRectMake(0, 0, self.view.width, 50);
        _header.image = [UIImage imageNamed:@"头视图背景"];
        
        UILabel *label = [[UILabel alloc] init];
        label.adjustsFontSizeToFitWidth = true;
        label.numberOfLines = 2;
        label.text = @"（您在商家的积分数据，平台将在第二天午时之前更新）";
        label.font = FONT(13);
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor blackColor];
        [_header addSubview:label];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(_header);
        }];
    }
    return _header;
}



@end
